package system.imp;

import org.apache.poi.hssf.usermodel.*;


/**
 * @author Jackey
 *
 * Excel通用读取类
 * 
 */
public class PoiUtil 
{
	
	public static int getCellType(HSSFCell cell)
	{
		int cellType = -1;
		
		try
		{
			cellType = cell.getCellType();
		}
		catch(Exception e)
		{
			cellType = -1;
		}
		
		return cellType;
	}
	
	
	public static String getCellValue(HSSFRow row, int i, String defaultValue)
	{
		int cellType = -1;
		String cellValue = "";
		
		try
		{
			HSSFCell cell = row.getCell((short)i);
			cellType = PoiUtil.getCellType(cell);
			
			if(cellType == HSSFCell.CELL_TYPE_STRING) cellValue = cell.getStringCellValue().trim();
				
			else if(cellType == HSSFCell.CELL_TYPE_NUMERIC)
			{
				double tp1 = cell.getNumericCellValue();
				if(tp1 - (long)tp1 == 0)
				{
					cellValue = String.valueOf((long)tp1);
				}
				else
				{
					cellValue = String.valueOf(tp1);
				}
			}
				
			else if(cellType == HSSFCell.CELL_TYPE_BOOLEAN) cellValue = String.valueOf(cell.getBooleanCellValue());
				
			else cellValue = defaultValue == null ? "" : defaultValue;
				
		}
		catch(Exception e)
		{
			cellValue = defaultValue == null ? "" : defaultValue;
		}
		
		return cellValue;
	}
	
    public static HSSFCell createCell(HSSFWorkbook aWb, HSSFRow aRow, short column,String aValue){
        HSSFCell tCell = aRow.createCell(column);
        tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        // 定义单元格为字符串类型
        tCell.setCellType(HSSFCell.CELL_TYPE_STRING);
        tCell.setCellValue(aValue);
        HSSFCellStyle tCellStyle = aWb.createCellStyle();
        
        tCell.setCellStyle(tCellStyle);
        return tCell;
    }

}
